Analyze Data & Create Plots
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(p8105.datasets)
library(plotly)
## Warning: package 'plotly' was built under R version 4.2.2
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
data("rest_inspec")
rest_inspec =
rest_inspec %>%
select(boro, cuisine_description, inspection_date,
critical_flag, score) %>%
filter(critical_flag != "NA", boro != "Missing", score != "NA")
#Scatter plot of mean score by borough over time
rest_inspec %>%
group_by(boro, inspection_date) %>%
summarize(mean_score = mean(score)) %>%
plot_ly(
x = ~inspection_date, y = ~mean_score, type = "scatter", mode = "markers",
color = ~boro, alpha = 0.5, colors = "viridis")
## `summarise()` has grouped output by 'boro'. You can override using the
## `.groups` argument.
#Bar chart of critical flags by cuisine
rest_inspec %>%
filter(critical_flag == "Critical") %>%
count(cuisine_description) %>%
mutate(critical_flag = fct_reorder(cuisine_description, n)) %>%
plot_ly(x = ~cuisine_description, y = ~n, type = "bar", color = ~cuisine_description, colors = "viridis")
#Boxplots of scores by criticality
rest_inspec %>%
plot_ly(x = ~critical_flag, y = ~score, type = "box", color = ~critical_flag, colors = "viridis")